<?php
/**
 * Created by PhpStorm.
 * User: sunzexin
 * Date: 2018/1/7
 * Time: 13:16
 */

namespace App\Http\Logics\Admin;


use App\Swiper;
use App\Http\Logics\BaseLogic;
class SwiperLogic extends BaseLogic
{
    public static function getSwiperList($data)
    {
        $query = new Swiper();
        $swiper = $data['limit'] ? $query->paginate($data['limit']) : $query->get();
        if (empty($swiper)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        return self::response(self::SUCCESS, $swiper);
    }

    public static function createSwiper($data)
    {
        try {
            $response = self::getPartnerIdByLoginSession($data['loginSession']);
            if ($response->code) {
                return self::response(self::CONTENT_IS_EMPTY);
            }
            $partner_id = $response->data;
            $swiper_list_arr = [];
            $swiper_list_arr_first = explode(',', $data['swiper_list_str']);
            foreach ($swiper_list_arr_first as $item) {
                $swiper_list_arr_second = explode('__', $item);
                array_push($swiper_list_arr, $swiper_list_arr_second);
            }
            foreach ($swiper_list_arr as $item) {
                $swiper = new Swiper();
                $swiper->partner_id = $partner_id;
                $swiper->cover = $item[0];
                $swiper->title = $item[1];
                $swiper->path = $item['2'];
                if (!$swiper->save()) {
                    return self::response(self::OBJECT_SAVE_FAIL);
                }
            }
            return self::response(self::SUCCESS);
        }catch (\Exception $e){
            return self::response(self::SYSTEM_ERROR);
        }
    }

    public static function sortSwiper($data)
    {
        try {
            $swiper_id_arr = explode(',', $data['swiper_id_str']);

            foreach ($swiper_id_arr as $key => $swiper_id) {
                $swiper = Swiper::find($swiper_id);
                $swiper->sort = $key + 1;
                if (!$swiper->save()) {
                    return self::response(self::OBJECT_SAVE_FAIL);
                }
            }

            return self::response(self::SUCCESS);
        }catch (\Exception $e){
            return self::response(self::SYSTEM_ERROR);
        }
    }

    /**
     * 删除轮播图
     * @param $data 筛选条件
     * @return mixed 返回结果
     * @throws \Exception
     */
    public static function deleteSwiper($data)
    {
        try {
            $swiper = Swiper::find($data['swiper_id']);
            if (empty($swiper)) {
                return self::response(self::CONTENT_IS_EMPTY);
            }
            if (!$swiper->delete()) {
                return self::response(self::OBJECT_DELETE_FAILD);
            }
            return self::response(self::SUCCESS);
        }catch (\Exception $e){
            return self::response(self::SYSTEM_ERROR);
        }
    }
}